import { Bit } from '@/utils/constant'
import { ApiInfo, ApiListOrderBy, ApiRequestType, ApiStatus } from '@/utils/eolink'
import R from './_util'

/** 获取项目信息  */
export const getProjectInfo = R.postForm<
  {
    type: 'share'
    statusCode: string
    projectName: string
    projectUpdateTime: string
    isLocked: 1
    projectVersion: string
    shareID: number
    viewSetting: {
      apiView: boolean
      apiTestView: number
      testCaseView: number
      mockApiView: number
      statusCodeView: boolean
      projectPageView: boolean
    }
    productType: number
  },
  {
    shareCode: string
    /** 固定值 project */
    shareType: 'project'
  }
>('/api/apiManagementPro/Share/getProjectInfo', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
})

/** 密码登录 */
export const verifySharePassword = R.postForm<
  {
    type: 'share'
    statusCode: string
    shareToken: string
  },
  {
    shareCode: string
    shareID: string
    sharePassword: string
  }
>('/api/apiManagementPro/Share/verifySharePassword', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
})

/** 校验token是否正确 */
export const verifyShareToken = R.postForm<
  {
    type: 'share'
    statusCode: '000000'
  },
  {
    shareCode: string
    shareID: string
    shareToken: string
  }
>('/api/apiManagementPro/Share/verifyShareToken', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
})

export const getApiGroupData = R.postForm<
  {
    apiGroupData: {
      apiList: {
        apiID: number
        apiName: string
        apiProtocol: 0
        apiRequestType: ApiRequestType
        apiType: 'http' | 'https'
      }[]
      groupID: number
      groupName: string
      groupOrder: string
      groupPath: string
      parentGroupID: number
    }[]
    groupOrder: string
    statusCode: string
    type: 'share'
  },
  {
    shareCode: string
    shareID: string
    shareToken: string
  }
>('/api/apiManagementPro/Share/getApiGroupData', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
})

/** 获取api列表 */
export const getApiList = R.postForm<
  {
    type: 'share'
    statusCode: '000000'
    itemNum: number
    apiList: {
      apiID: number
      apiName: string
      apiURI: string
      apiStatus: ApiStatus
      apiRequestType: ApiRequestType
      createTime: string
      apiUpdateTime: string
      starred: number
      groupID: number
      parentGroupID: number
      groupName: string
      apiType: 'http' | 'https'
      apiProtocol: 0
      apiTag: string
    }[]
  },
  {
    shareCode: string
    shareID: string
    groupId: string
    shareToken: string
    shareType: 'project'
    orderBy: ApiListOrderBy
    asc: Bit
    pageSize: number
    page: number
    /** 未知，固定传0 */
    condition: 0
  }
>('/api/apiManagementPro/Share/getApiList', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
})

export const searchApi = R.postForm<
  R.IResponse<typeof getApiList>,
  R.IParams<typeof getApiList> & {
    keyword: string
    /** 与keyword相同，可能是为了兼容 */
    tip: string
  }
>('/api/apiManagementPro/Share/searchApi', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  },
})

export const getApiInfo = R.postForm<{
  type: 'share'
  statusCode: string
  apiInfo: ApiInfo.IApiInfo
}, {
  apiID: string
  shareCode: string
  shareID: string
  groupID: string
  /** 固定值 project */
  shareType: 'project'
  shareToken: string
}>('/api/apiManagementPro/Share/getApi', {
  headers: {
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
  }
})